<template>
	<!--  投料消耗查询-->
	<div class="content">
		<div class="pageSearchView">
			<el-form :inline="true" :model="searchParam" class="demo-form-inline">
				<el-form-item label="工序订单号">
					<el-input v-model="searchParam.procedureOrderNo" placeholder="请输入工序订单号" clearable style="width: 200px" />
				</el-form-item>
				<el-form-item label="工单号">
					<el-input v-model="searchParam.workNo" placeholder="请输入工单号" clearable style="width: 200px" />
				</el-form-item>
				<el-form-item label="批次号">
					<el-input v-model="searchParam.batchNo" placeholder="请输入工单号" clearable style="width: 200px" />
				</el-form-item>
				<el-form-item label="工序名称">
					<el-input v-model="searchParam.procedureName" placeholder="请输入工序名称" clearable style="width: 200px" />
				</el-form-item>
				<el-form-item label="物料编码">
					<el-input v-model="searchParam.materialNo" placeholder="请输入物料编码" clearable style="width: 200px" />
				</el-form-item>
				<el-form-item label="计划号">
					<el-input v-model="searchParam.planOrderNo" placeholder="请输入计划号" clearable style="width: 200px" />
				</el-form-item>
				<el-form-item label="车间/产线">
					<el-tree-select v-model="searchParam.organizationId" :data="workRoomList" check-strictly
													:props="{ children: 'sonList', label: 'orgName', value: 'id' }" style="width: 300px" />
				</el-form-item>
				<!-- <el-form-item label="来源单号">
					<el-input v-model="searchParam.sourceNo" placeholder="请输入来源单号" clearable style="width: 200px" />
				</el-form-item> -->
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
					<el-button class="refreshBtn" :icon="Refresh" @click="resetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="pageTableView">
			<div class="pageTableMenuView">
				<div class="pageTableTitle">列表信息</div>
			</div>
			<!-- 表格 -->
			<el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 320px)">
				<el-table-column label="序号" align="center" width="70">
					<template #default="scope">
						<!-- 下标 + 1，计算公式：当前页数 * 每页大小 + 当前行的索引 + 1 --> {{ (pageSearch.current - 1) * pageSearch.pageSize +
					scope.$index + 1 }}
					</template>
				</el-table-column>
				<el-table-column prop="workNo" label="工单号" align="center" min-width="200" />
				<el-table-column prop="procedureOrderNo" label="工序订单号" align="center" min-width="200" />
				<el-table-column prop="materialNo" label="物料编号" align="center" show-overflow-tooltip min-width="200" />
				<el-table-column prop="batchNo" label="批次号" align="center" min-width="200" />
				<el-table-column prop="traceCode" label="追溯码" align="center" min-width="280" show-overflow-tooltip />
				<el-table-column prop="count" label="扣除数量" align="center" min-width="200" />
				<el-table-column prop="surplusCount" label="剩余数量" align="center" min-width="200" />
				<el-table-column prop="repoetCount" label="报工数量" align="center" min-width="200" />
				<el-table-column prop="planOrderNo" label="计划号" align="center" min-width="200" />
				<el-table-column prop="status" label="消耗状态" align="center" min-width="200">
					<template #default="scope">
						{{ scope.row.status == 0 ? '部分消耗' : '全部消耗' }}
					</template>
				</el-table-column>
				<!--       <el-table-column prop="procedureOrderNo" label="生产订单号" align="center" min-width="200" />-->
				<!--       <el-table-column prop="storageId" label="产线/车间" align="center" min-width="200" show-overflow-tooltip />-->
				<el-table-column prop="createUserName" label="创建人" align="center" min-width="200" />
				<el-table-column prop="createTime" label="创建时间" align="center" min-width="200" />

			</el-table>
			<div class="pagePagination">
				<el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
											 :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next"
											 :total="pageTotal"
											 @size-change="handleSizeChange" @current-change="handleCurrentChange" />
			</div>
		</div>
	</div>
</template>
<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue';

onMounted(() => {
	getList();
	getOption();
});


const pageSearch = ref({
	current: 1,
	pageSize: 10
});


const workOrderList = ref({});


const pageTotal = ref(null);//页面总数
const searchParam = ref({});//页面检索条件

const workRoomList = ref([]);//车间

// 判断质检类型
const getQualityTypeByValue = (e) => {
	let name = null;
	for (const item of qualityTypeList) {
		if (e == item.value) {
			name = item.label;
		}
	}
	return name;
};

// 质检类型
const qualityTypeList = reactive([
	{
		label: '部分送货',
		value: '1'
	},
	{
		label: '全部送货',
		value: '2'
	},
	{
		label: '未送货',
		value: '0'
	}
	// 继续添加更多检查类型...
]);


const paginatedData = ref([]);
const materiaList = ref([]);//物料表
const wareList = ref([]);//仓库
const procedureOrderList = ref([]);//工序订单
const supplierList = ref([]);
const storageAllList = ref([]);
// 供应商远端搜索
const supplierSearch = async (query: string) => {
	if (query) {
		try {
			const response = await axiosInstance.post(`/api/basedataSupplier/getPage`, {
				current: 1,
				pageSize: 20,
				name: query
			});
			supplierList.value = await response.data.records; // 假设返回的数据直接是供应商列表
		} catch (error) {
			supplierList.value = []; // 出错时清空列表
		}
	}
};

const getOption = () => {
	// axiosInstance.post('/api/outsourcingPlan/querySourcingPlanProcedureOrder', {})
	//   .then(res => {
	//     workOrderList.value = res.data;
	//   });
	//
	// // 查询所有库位
	// axiosInstance.post('/api/basedataStorage/getAll', {})
	//   .then(res => {
	//     storageAllList.value = res.data;
	//   });
	//
	//
	// axiosInstance.post(`/api/basedataSupplier/getPage`, {
	//   current: 1,
	//   pageSize: 20
	// }).then(res => {
	//   if (res.code == 200) {
	//     supplierList.value = res.data.records;
	//   }
	// });
	//
	//
	// axiosInstance.post('/api/planProcedureOrder/getAll', {})
	//   .then(res => {
	//     if (res.code !== 200) {
	//       ElMessage.error(res.message);
	//       return false;
	//     }
	//     const list = res.data;
	//     const loop = [];
	//     for (const item of list) {
	//       loop.push(
	//         {
	//           value: item.id,
	//           label: item.materialName
	//         }
	//       );
	//     }
	//     procedureOrderList.value = list;
	//   });
	//
	// axiosInstance.get('/api/basedataWarehouse/treeList', {}).then(res => {
	//   if (res.code !== 200) {
	//     ElMessage.error(res.message);
	//     return false;
	//   }
	//   wareList.value = res.data;
	// });

	axiosInstance.post('/api/basedataOrganization/getAll', {})
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			workRoomList.value = res.data[0].sonList
		});
};

// 获取列表接口
const getList = () => {
	axiosInstance.post('/api/reportWorkRecordDeductingRawMaterialsRecords/getPage', {
		...searchParam.value,
		...pageSearch.value
	})
		.then(res => {
			if (res.code !== 200) {
				ElMessage.error(res.message);
				return false;
			}
			paginatedData.value = res.data.records;
			pageTotal.value = res.data.total;
		});
};

const handleSizeChange = (e) => {
	pageSearch.value.pageSize = e;
	getList();
};

const handleCurrentChange = (e) => {
	pageSearch.value.current = e;
	getList();
};

// 重置搜索条件
const resetSearch = () => {
	searchParam.value = {};
	pageSearch.value.current = 1;
	// current.value = 1
	getList();
};

</script>
<style scoped></style>
